package com.demo1;

import java.io.File;
import java.io.IOException;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;

import com.demo.utils.LuceneUtils;

public class LuceneTest1 {

	@Test public void deleteIndexAll() throws IOException{
		IndexWriter indexWriter=LuceneUtils.getIndexWriter();
		//删除所有索引
		indexWriter.deleteAll();
		indexWriter.commit();
		indexWriter.close();
	}
	
	
	
	@Test public void updateIndex() throws IOException{
		 Document doc = new Document();
		 doc.add(new StringField("id","44444",Store.YES));
		 doc.add(new StringField("title","updateDocument测试 4",Store.YES));
		 doc.add(new TextField("content","updateDocument测试 4",Store.YES));
		 IndexWriter indexWriter=LuceneUtils.getIndexWriter();
		 //使用doc来覆盖索引库里字段id的值等于1的那条记录；当索引库中没有那条记录是，执行新增操作，向索引库插入一条记录
		 indexWriter.updateDocument(new Term("id","3"), doc);
		 indexWriter.commit();
		 indexWriter.close();
	}
	
	
	@Test public void queryIndex() throws Exception{
		//查询的内容 lucene
		String searchContent="lucene";
		//查询的字段title
		QueryParser queryParser=new QueryParser("title", new StandardAnalyzer());
		Query query = queryParser.parse(searchContent);
		//查询的索引库 d:\indexDir
		//索引查询器
		IndexSearcher indexSearcher=new IndexSearcher(DirectoryReader.open(FSDirectory.open(new File("d:\\indexDir"))));
		//执行查询，得到得分文档TopDocs集合
		TopDocs topDocs=indexSearcher.search(query, Integer.MAX_VALUE);
		System.out.println("查询的结果数为:"+topDocs.totalHits);
		for (ScoreDoc scoreDoc:topDocs.scoreDocs) {
			//取出lucene为数据设置的唯一标识id
			int docId=scoreDoc.doc;
			System.out.println("文档ID:"+docId);
			Document doc = indexSearcher.doc(docId);
			//取文档内容
			System.out.println(doc.get("id"));
			System.out.println(doc.get("title"));
			System.out.println(doc.get("content"));
		}
	}
	
	@Test public void createIndex() throws Exception{
		//指定文件目录
		Directory directory = FSDirectory.open(new File("d:\\indexDir"));
		//创建分词器对象
		Analyzer analyzer = new StandardAnalyzer();
		//indexWriter配置:指定lucene版本，指定使用何种分词器
		IndexWriterConfig config = new IndexWriterConfig(Version.LATEST, analyzer);
		//创建索引写入器
		IndexWriter indexWriter =new IndexWriter(directory, config);
		Document document=new Document();
		document.add(new LongField("id",1L,Store.YES));
		document.add(new StringField("title","lucene",Store.YES));
		document.add(new TextField("content","lucene 是全文索引和搜寻的开源API", Store.YES));
		
		indexWriter.addDocument(document);
		indexWriter.commit();
		indexWriter.close();
	}
}
